package Date;


import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.ValueRange;

/**
 * 2020-08-11作业
 */
public class Practice {

    /**
     * 上月第一天是周几
     */
    @Test
    public void demo1() {
        LocalDate localDate = LocalDate.now();
        int dayOfWeek = localDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).getDayOfWeek().getValue();
        //geDayOfWeek() 是Wednesday
        //getValue() 是数字3
        System.out.println(dayOfWeek);
    }

    /**
     * 下月最后一天是周几
     */
    @Test
    public void demo2() {
        LocalDate localDate = LocalDate.now();
        int dayOfWeek = localDate.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek().getValue();
        System.out.println(dayOfWeek);
    }

    /**
     * 获取上月第一天开始，形如：2020-08-01 00:00:00
     */
    @Test
    public void demo3() {
        LocalDateTime localDateTime = LocalDateTime.now();
        String day = localDateTime.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()) //with(ChronoField.DAY_OF_MONTH,1)也可以
                .withHour(0).withMinute(0).withSecond(0).withNano(0)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(day);
    }

    /**
     * 获取下月最后一天的结束时间，形如：2020-08-31 23:59:59
     */
    @Test
    public void demo4() {
        LocalDateTime localDateTime = LocalDateTime.now();
        String day = localDateTime.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth())
                .withHour(0).withMinute(0).withSecond(0).withNano(0)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(day);
    }

    /**
     * 任意给定年份，获取该年二月有多少天
     */
    @Test
    public void demo5() {
        int year = 2020;
        LocalDate localDate = LocalDate.of(year, 2, 1);
        long days = localDate.range(ChronoField.DAY_OF_MONTH).getMaximum();
//        ValueRange range = localDate.range(ChronoField.DAY_OF_MONTH);
//        System.out.println(range.getMaximum());
        System.out.println(days);
    }

    /**
     * 获取母亲节日期
     */
    @Test
    public void demo6() {
        int year = 2021;
        LocalDate localDate = LocalDate.of(year, 5, 1);
        System.out.println(localDate.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SUNDAY)));
    }

    /**
     * 图片验证
     */
//    @Test
//    public void demo7() {
//        String pictureName = "a.png1";
//        String newName = null;
//        try {
//            newName = PictureUtils.checkAndRenamePicture(pictureName);
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(e.getMessage());
//        }
//        System.out.println(newName);
//
//    }


}
